//
//  CityLocationAction.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "CityLocationAction.h"
#import "ClientApi.h"
@implementation CityLocationAction
-(instancetype)init{
    
    if(self = [super init]){
        
        _dataArray = [[NSMutableArray alloc] init];
        _hotArray = [[NSMutableArray alloc] init];
        _letterArray = [[NSMutableArray alloc] init];
        
    }
    
    return self;
}


-(void) start:(void (^)()) completeBlock{
    
    NSArray * cletters = [CityInfo getCacheLetterArray];
    NSArray * chots = [CityInfo getCacheHotCityList];
    NSArray * calls = [CityInfo getCacheAllCityList];
    
    if(cletters && [cletters count]> 0){
        
        [self.letterArray removeAllObjects];
        [self.letterArray addObjectsFromArray:cletters];
    }
    
    if(chots && [chots count] > 0){
        
        [self.hotArray removeAllObjects];
        [self.hotArray addObjectsFromArray:chots];
    }
    
    if(calls && [calls count] > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:calls];
        
    }
    
    
    
    [[ClientApi getInstance] getXindaiZone:1 withHot:1 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if(!error){
            
            [self.dataArray removeAllObjects];
            [self.hotArray removeAllObjects];
            [self.letterArray removeAllObjects];
            
            //[letterArray addObject:@"热"];
            [self.letterArray addObjectsFromArray:[responseObject valueForKey:@"letters"]];
            //            letterArray = [responseObject r:@"letters"];
            [self.dataArray addObjectsFromArray:[CityInfo getAllCityList:responseObject]];
            [self.hotArray addObjectsFromArray:[CityInfo getHotCityList:[responseObject valueForKey:@"hots"]]];
            if(responseObject){
                
                [CityInfo cacheCityList:responseObject];
            }
            
            completeBlock();
            
        }else{
            
            GYNLog(@"error ~");
        }
        
    }];
    
}


-(CityInfo*)getCityItem:(NSIndexPath*)indexPath{
    id dic   = [self.dataArray objectAtIndex:[indexPath section]];
    NSArray * subArray = [dic valueForKey:@"subs"];
    CityInfo * city = [subArray objectAtIndex:[indexPath row]];
    return city;
}



@end
